<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item :label="`${$branchName?$branchName:'分院'}名称`" prop="branchId">
        <el-select
          v-model="queryParams.branchId"
          :placeholder="`${$branchName?$branchName:'分院'}名称`"
          filterable
          clearable
        >
          <el-option
            v-for="(item, index) in branchs"
            :key="index"
            :label="item.branchName"
            :value="item.branchId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-preventReClick
                   type="primary"
                   icon="el-icon-search"
                   size="mini"
                   @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button v-preventReClick icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="postList">
      <el-table-column :label="`${$branchName?$branchName:'分院'}名称`" align="center" prop="branchName"/>
      <el-table-column label="必修" align="center">
        <template slot-scope="scope">
          <span>{{
              scope.row.compulsoryCredit ? scope.row.compulsoryCredit : 0
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="选修" align="center">
        <template slot-scope="scope">
          <span>{{
              scope.row.compulsoryCredit ? scope.row.optionalCredit : 0
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考证" align="center">
        <template slot-scope="scope">
          <span>{{
              scope.row.compulsoryCredit ? scope.row.competitionCredit : 0
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工学" align="center">
        <template slot-scope="scope">
          <span>{{
              scope.row.compulsoryCredit ? scope.row.engineeringCredit : 0
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总学分" align="center">
        <template slot-scope="scope">
          <span>{{
              (scope.row.compulsoryCredit ? scope.row.compulsoryCredit : 0) +
              (scope.row.optionalCredit ? scope.row.optionalCredit : 0) +
              (scope.row.competitionCredit ? scope.row.competitionCredit : 0) +
              (scope.row.engineeringCredit ? scope.row.engineeringCredit : 0)
            }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button v-preventReClick
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
          >编辑
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title" :visible.sync="open" width="500px" append-to-body
    >
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学院名称" prop="postSort">
          {{ form.branchName }}
        </el-form-item>
        <el-form-item label="必修学分" prop="compulsoryCredit">
          <el-input-number
            :step="0.1"
            :precision="1"
            v-model="form.compulsoryCredit"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="选修学分" prop="optionalCredit">
          <el-input-number
            :step="0.1"
            :precision="1"
            v-model="form.optionalCredit"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="考证学分" prop="competitionCredit">
          <el-input-number
            :step="0.1"
            :precision="1"
            v-model="form.competitionCredit"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="工学学分" prop="engineeringCredit">
          <el-input-number
            :step="0.1"
            :precision="1"
            v-model="form.engineeringCredit"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="总分" prop="postSort">
          <el-input-number
            :step="0.1"
            :precision="1"
            disabled
            :value="
              (form.compulsoryCredit ? form.compulsoryCredit : 0) +
              (form.optionalCredit ? form.optionalCredit : 0) +
              (form.competitionCredit ? form.competitionCredit : 0) +
              (form.engineeringCredit ? form.engineeringCredit : 0)
            "
            controls-position="right"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm">确 定</el-button>

      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPost_branch,
  updatePost_branch,
  getPost_branch
} from '@/api/schoolOrganization/organization'

export default {
  name: 'Post',
  data() {
    return {
      branchs: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        branchId: undefined
      },
      // 表单参数
      form: {
        branchId: undefined,
        branchName: undefined,
        compulsoryCredit: undefined,
        optionalCredit: undefined,
        competitionCredit: undefined,
        engineeringCredit: undefined
      },
      // 表单校验
      rules: {
        compulsoryCredit: [
          { required: true, message: '必修学分不能为空', trigger: 'blur' }
        ],
        optionalCredit: [
          { required: true, message: '选修学分不能为空', trigger: 'blur' }
        ],
        competitionCredit: [
          { required: true, message: '考证学分不能为空', trigger: 'blur' }
        ],
        engineeringCredit: [
          { required: true, message: '工学学分不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getBranchs()
  },
  methods: {
    /* 获取总学院 */
    getBranchs() {
      listPost_branch().then((response) => {
        this.branchs = response.rows

      })
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listPost_branch(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        branchId: undefined,
        branchName: undefined,
        compulsoryCredit: undefined,
        optionalCredit: undefined,
        competitionCredit: undefined,
        engineeringCredit: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const postId = row.branchId || this.ids
      getPost_branch(postId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '学分配置'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          updatePost_branch(this.form).then(() => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
